package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil; 
import java.math.BigDecimal;
import java.util.Calendar;

@Repository("PurchasematerialDAO")
@Transactional
public class PurchasematerialDAOImpl extends AbstractJpaDao<Purchasematerial> implements PurchasematerialDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Purchasematerial.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public PurchasematerialDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Purchasematerial findPurchasematerialById(Integer id) throws DataAccessException {

		return findPurchasematerialById(id, -1, -1);
	}

	@Transactional
	public Purchasematerial findPurchasematerialById(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findPurchasematerialById", startResult, maxRows, id);
			return (Purchasematerial) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Purchasematerial> findPurchasematerialByName(String name) throws DataAccessException {

		return findPurchasematerialByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Purchasematerial> findPurchasematerialByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findPurchasematerialByName", startResult, maxRows, name);
		return new LinkedHashSet<Purchasematerial>(query.getResultList());
	}

	@Transactional
	public Set<Purchasematerial> findPurchasematerialByNameContaining(String name) throws DataAccessException {

		return findPurchasematerialByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Purchasematerial> findPurchasematerialByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findPurchasematerialByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Purchasematerial>(query.getResultList());
	}

	@Transactional
	public Set<Purchasematerial> findPurchasematerialByDate(Calendar date) throws DataAccessException {

		return findPurchasematerialByDate(date, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Purchasematerial> findPurchasematerialByDate(Calendar date, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findPurchasematerialByDate", startResult, maxRows, date);
		return new LinkedHashSet<Purchasematerial>(query.getResultList());
	}

	@Transactional
	public Set<Purchasematerial> findPurchasematerialByPrice(BigDecimal price) throws DataAccessException {

		return findPurchasematerialByPrice(price, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Purchasematerial> findPurchasematerialByPrice(BigDecimal price, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findPurchasematerialByPrice", startResult, maxRows, price);
		return new LinkedHashSet<Purchasematerial>(query.getResultList());
	}

	@Transactional
	public Set<Purchasematerial> findPurchasematerialByQuantity(BigDecimal quantity) throws DataAccessException {

		return findPurchasematerialByQuantity(quantity, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Purchasematerial> findPurchasematerialByQuantity(BigDecimal quantity, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findPurchasematerialByQuantity", startResult, maxRows, quantity);
		return new LinkedHashSet<Purchasematerial>(query.getResultList());
	}

	@Transactional
	public Set<Purchasematerial> findPurchasematerialByDescription(String description) throws DataAccessException {

		return findPurchasematerialByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Purchasematerial> findPurchasematerialByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findPurchasematerialByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Purchasematerial>(query.getResultList());
	}

	@Transactional
	public Set<Purchasematerial> findPurchasematerialByDescriptionContaining(String description)
			throws DataAccessException {

		return findPurchasematerialByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Purchasematerial> findPurchasematerialByDescriptionContaining(String description, int startResult,
			int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPurchasematerialByDescriptionContaining", startResult, maxRows,
				description);
		return new LinkedHashSet<Purchasematerial>(query.getResultList());
	}

	@Transactional
	public Purchasematerial findPurchasematerialByPrimaryKey(Integer id) throws DataAccessException {
		return findPurchasematerialByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Purchasematerial findPurchasematerialByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findPurchasematerialByPrimaryKey", startResult, maxRows, id);
			return (Purchasematerial) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Purchasematerial> findPurchasematerialsByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findPurchasematerialsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Purchasematerial> findAllPurchasematerials() throws DataAccessException {

		return findAllPurchasematerials(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Purchasematerial> findAllPurchasematerials(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllPurchasematerials", startResult, maxRows);
		return new LinkedHashSet<Purchasematerial>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Purchasematerial> findPurchasematerialsByMaterial(Integer materialId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllPurchasematerialsByMaterial", start, max, materialId);
		return new LinkedHashSet<Purchasematerial>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Purchasematerial> findPurchasematerialsByProducer(Integer producerId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllPurchasematerialsByProducer", start, max, producerId);
		return new LinkedHashSet<Purchasematerial>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Purchasematerial> findPurchasematerialsBySupplier(Integer supplierId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllPurchasematerialsBySupplier", start, max, supplierId);
		return new LinkedHashSet<Purchasematerial>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Purchasematerial> findPurchasematerialsByUnit(Integer unitId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllPurchasematerialsByUnit", start, max, unitId);
		return new LinkedHashSet<Purchasematerial>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Purchasematerial> findPurchasematerialByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Purchasematerial>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Purchasematerial> findPurchasematerialsByDetachedCriteria(CmiPagination page)
			throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Purchasematerial.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Purchasematerial> purchasematerials = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return purchasematerials;
	}

	@Transactional
	public Purchasematerial findPurchasematerialByPhysicalPrimaryKey(Integer producerId, Integer supplierId,
			Integer materialId, Calendar date) throws DataAccessException {
		return findPurchasematerialByPhysicalPrimaryKey(producerId, supplierId, materialId, date, -1, -1);
	}

	@Transactional
	public Purchasematerial findPurchasematerialByPhysicalPrimaryKey(Integer producerId, Integer supplierId,
			Integer materialId, Calendar date, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findPurchasematerialByPhyscialPrimaryKey", startResult, maxRows, producerId,
					supplierId, materialId, date);
			return (Purchasematerial) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Purchasematerial entity) {
		return true;
	}
}
